class SmsAddController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, UserService, getCompanyList, iRunner){

        var smsadd = this;

        smsadd.$rootScope     = $rootScope;
        smsadd.$cookieStore   = $cookieStore;
        smsadd.$timeout       = $timeout;
        smsadd.$state         = $state;
        smsadd.ngDialog       = ngDialog;
        smsadd.dialogService  = dialogService;
        smsadd.UserService = UserService;
        smsadd.iRunner        = iRunner;
        smsadd.getCompanyList = getCompanyList;


        // 金额换算成点数
        $scope.moneyToPoint = function() {
            // 限制最大输入值为50000
            if (smsadd.money > 50000) {
                smsadd.ngDialog.open({
                    template: '<p>最大输入值为50000</p>',
                    className: 'ngdialog-theme-default ngdialog-alert',
                    plain: true
                });
                smsadd.money = 50000;
            }

            smsadd.opint = smsadd.money * 100;
            smsadd.numview = '您大约充值' + smsadd.money * 10 + '条短信';
            smsadd.num = smsadd.money * 10;
        };
        // 点数换算条数
        $scope.pointToNum = function() {
            smsadd.numview = '您大约充值' + smsadd.opint / 10 + '条短信';
            smsadd.num = smsadd.opint / 10;
        };

        // 表单提交
        smsadd.submitForm = function(isValid) {

            if (!isValid) {
                smsadd.ngDialog.open({
                    template: '<p>请选择要充值短信的机构</p>',
                    className: 'ngdialog-theme-default ngdialog-alert',
                    plain: true
                });

                return false;
            }

            //机构id
            //smsadd.company.company_id
            if (!smsadd.opint || !smsadd.money) {
                // alert("请正确填写充值短信条数");
                smsadd.ngDialog.open({
                    template: '<p>请正确填写充值金额和充值点数</p>',
                    className: 'ngdialog-theme-default ngdialog-alert',
                    plain: true
                });

            } else {
                //模态框确认充值信息
                smsadd.ngDialog.open({
                    template: "<p>确定要给 " + smsadd.company.company_name + " 充值短信 " + smsadd.num + " 条吗？</p><br><button class='btn btn-primary' ng-click='smsadd()'>确定</button>",
                    className: 'ngdialog-theme-default ngdialog-alert',
                    plain: true,
                    scope: $rootScope
                });
            }

        };

        // 重新载入页面
        $rootScope.reload = function() {
            //关闭确认充值模态框
            smsadd.ngDialog.close();
            // 重新载入页面
            smsadd.$state.reload();
        };

        // 短信充值
        $rootScope.smsadd = function() {
            // alert(smsadd.money + smsadd.opint + smsadd.company.company_id);

            var params = {
                company_id: smsadd.company.company_id,
                pay_money: smsadd.money,
                points: smsadd.opint,
                desc: smsadd.des 
            };
            //关闭确认充值模态框
            smsadd.ngDialog.close();

            smsadd.UserService.postSmsadd(params ,smsadd.$cookieStore.get('access_token'))
            .then(function(data) {
                //充值成功
                if (data.code == 0) {
                    smsadd.ngDialog.open({
                        template: "<p>充值成功</p><br><button class='btn btn-primary' ng-click='reload()'>确定</button>",
                        className: 'ngdialog-theme-default ngdialog-alert',
                        plain: true,
                        scope: $rootScope

                    });

                } else {
                    smsadd.ngDialog.open({
                        template: '<p>' + data.message + '</p>',
                        className: 'ngdialog-theme-default ngdialog-alert',
                        plain: true
                    });
                }
                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
                console.log(data)
            }, function(data) {
                console.log('error')
            });

        };
    }
}

SmsAddController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'UserService', 'getCompanyList', 'iRunner']; 

export default SmsAddController;